﻿using HuiZe.education.Model;
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace HuiZe.education.View
{
    /// <summary>
    /// PART_MyShare_Item2.xaml 的交互逻辑
    /// </summary>
    public partial class PART_MyShare_Item2 : UserControl
    {
        public resShareList Data
        {
            get { return (resShareList)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataProperty =
            DependencyProperty.Register("Data", typeof(resShareList), typeof(PART_MyShare_Item2), new PropertyMetadata(null, (sender, e) =>
             {
                 PART_MyShare_Item2 _item = (PART_MyShare_Item2)sender;
                 resShareList _data = null;
                 if (e.NewValue != null && e.NewValue is resShareList)
                     _data = (resShareList)e.NewValue;

                 _item.Load(_data);
             }));

        public Action<resShareList> DeleteCallback { get; set; }

        public PART_MyShare_Item2()
        {
            InitializeComponent();
            MouseEnter += PART_MyShare_Item2_MouseEnter;
            MouseLeave += PART_MyShare_Item2_MouseLeave;
            PART_Delete.MouseLeftButtonUp += PART_Delete_MouseLeftButtonUp;
        }

        void PART_MyShare_Item2_MouseLeave(object sender, MouseEventArgs e)
        {
            PART_Bar.Visibility = Visibility.Collapsed;
        }

        void PART_MyShare_Item2_MouseEnter(object sender, MouseEventArgs e)
        {
            PART_Bar.Visibility = Visibility.Visible;
        }

        void PART_Delete_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (DeleteCallback != null)
                DeleteCallback(this.Data);
        }

        public void Load(resShareList _data)
        {
            if (_data == null) return;

            //BitmapImage _bmp = new BitmapImage();
            //_bmp.CacheOption = BitmapCacheOption.OnLoad;
            //_bmp.BeginInit();
            //_bmp.UriSource = new Uri(_data.image_cover, UriKind.RelativeOrAbsolute);
            //_bmp.EndInit();

            //PART_Icon.ImageSource = _bmp;

            //Load_Image(_data.image_cover);
            PART_Title.Text = _data.title;
            PART_Count.Text = _data.number.ToString();
            PART_Time.Text = _data.createtime_text;
            PART_Type.Text = _data.share_type == "1" ? "全网" : "本校";
        }

        public void Load_Image()
        {
            if (Data == null || string.IsNullOrEmpty(Data.image_cover)) return;

            Load_Image(Data.image_cover);
        }


        void Load_Image(string _url)
        {
            using (var _client = new WebClient())
            {
                _client.Proxy = null;
                _client.DownloadDataCompleted += (s, e) =>
                {
                    if (e.Result != null && e.Result.Length > 0)
                    {
                        try
                        {
                            BitmapImage _bmp = new BitmapImage();
                            _bmp.CacheOption = BitmapCacheOption.OnLoad;
                            _bmp.BeginInit();
                            _bmp.StreamSource = new System.IO.MemoryStream(e.Result);
                            _bmp.EndInit();

                            PART_Icon.ImageSource = _bmp;
                        }
                        catch (Exception ex)
                        {
                            App.Log(ex);
                        }
                    }
                };
                _client.DownloadDataAsync(new Uri(_url, UriKind.RelativeOrAbsolute));
            }
        }
    }
}
